package advancedFeatures;

/**
 * 泛型边界（约束）概念演示
 * 1. 上界约束
 * 2. 多重边界
 * 3. 类型参数约束
 */
public class GenericBounds {
    // 上界约束示例
    public static <T extends Number> double sum(T[] array) {
        double sum = 0.0;
        for (T item : array) {
            sum += item.doubleValue();
        }
        return sum;
    }
    
    // 多重边界示例
    public static <T extends Number & Comparable<T>> T findMax(T[] array) {
        if (array == null || array.length == 0) return null;
        
        T max = array[0];
        for (T item : array) {
            if (item.compareTo(max) > 0) {
                max = item;
            }
        }
        return max;
    }
    
    // 泛型类中的边界约束
    public static class ComparableContainer<T extends Comparable<T>> {
        private T value;
        
        public boolean isGreaterThan(T other) {
            return value.compareTo(other) > 0;
        }
    }
    
    public static void main(String[] args) {
        Integer[] integers = {1, 2, 3, 4, 5};
        Double[] doubles = {1.1, 2.2, 3.3, 4.4, 5.5};
        
        System.out.println("整数数组的和：" + sum(integers));
        System.out.println("浮点数组的和：" + sum(doubles));
        
        System.out.println("整数数组的最大值：" + findMax(integers));
        System.out.println("浮点数组的最大值：" + findMax(doubles));
    }
} 